import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'file_utils.dart';

class IconPainter {
  static Future<void> saveIcon() async {
    final recorder = ui.PictureRecorder();
    final canvas = Canvas(recorder);
    final size = const Size(1024, 1024);

    // 绘制背景
    final paint = Paint()..color = const Color(0xFF2196F3);
    canvas.drawCircle(size.center(Offset.zero), size.width / 2, paint);

    // 绘制白色对话框
    paint.color = Colors.white;
    final leftBubble = RRect.fromRectAndRadius(
      Rect.fromLTWH(size.width * 0.2, size.height * 0.3, size.width * 0.25,
          size.height * 0.3),
      const Radius.circular(20),
    );
    canvas.drawRRect(leftBubble, paint);

    final rightBubble = RRect.fromRectAndRadius(
      Rect.fromLTWH(size.width * 0.55, size.height * 0.4, size.width * 0.25,
          size.height * 0.3),
      const Radius.circular(20),
    );
    canvas.drawRRect(rightBubble, paint);

    // 绘制箭头
    paint.color = Colors.white;
    paint.strokeWidth = 15;
    paint.style = PaintingStyle.stroke;
    paint.strokeCap = StrokeCap.round;

    final path = Path();
    final centerX = size.width * 0.5;
    final centerY = size.height * 0.5;

    // 左箭头
    path.moveTo(centerX - 50, centerY - 20);
    path.lineTo(centerX - 80, centerY);
    path.lineTo(centerX - 50, centerY + 20);

    // 右箭头
    path.moveTo(centerX + 50, centerY - 20);
    path.lineTo(centerX + 80, centerY);
    path.lineTo(centerX + 50, centerY + 20);

    canvas.drawPath(path, paint);

    // 绘制连接线
    paint.strokeWidth = 10;
    canvas.drawLine(
      Offset(centerX - 80, centerY),
      Offset(centerX + 80, centerY),
      paint,
    );

    // 将绘制内容保存为图片
    final picture = recorder.endRecording();
    final image =
        await picture.toImage(size.width.toInt(), size.height.toInt());
    final byteData = await image.toByteData(format: ui.ImageByteFormat.png);

    if (byteData != null) {
      final bytes = byteData.buffer.asUint8List();
      await FileUtils.saveBytes('assets/icons/app_icon.png', bytes);
    }
  }
}
